package cn.operation.backend.service;

import cn.operation.backend.enums.DeleteStatusEnum;
import cn.operation.base.mapper.ItfLatestCodePondMapper;
import cn.operation.base.mapper.SOrderMapper;
import cn.operation.base.model.ItfLatestCodePond;
import cn.operation.base.model.ItfLatestCodePondExample;
import cn.operation.base.model.OrderDetailDO;
import cn.operation.base.model.ServiceOrderDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderServiceImpl.class);
    @Autowired
    private SOrderMapper sOrderMapper;
    @Autowired
    private ItfLatestCodePondMapper itfLatestCodePondMapper;
    @Override
    public OrderDetailDO queryBySSOrderCode(String orderCode) throws Exception {
        LOGGER.info("OrderServiceImpl.queryBySSOrderCode params ：{}",orderCode);
        OrderDetailDO orderDetailDO=new OrderDetailDO();
        try{
            orderDetailDO = sOrderMapper.getOrderDetailByOrderCode(orderCode);
        }catch (Exception e){
            LOGGER.info("OrderServiceImpl.queryBySSOrderCode error ：{} 查询失败");
        }
        try {
            ItfLatestCodePondExample example = new ItfLatestCodePondExample();
            example.createCriteria().andCodeEqualTo(orderCode).andStatusEqualTo(DeleteStatusEnum.NORMAL.getValue());
            List<ItfLatestCodePond> list = itfLatestCodePondMapper.selectByExample(example);
            if (list.size()>0) {
                ItfLatestCodePond codePond = new ItfLatestCodePond();
                codePond.setId(list.get(0).getId());
                codePond.setStatus(DeleteStatusEnum.DELETRE.getValue());
                itfLatestCodePondMapper.updateByPrimaryKeySelective(codePond);
            }
        } catch (Exception e) {
            LOGGER.error("OrderServiceImpl.queryBySSOrderCode param update ItfLatestCodePond data error: ");
            throw new IllegalArgumentException("修改业务单据号码池单据状态为删除状态异常:"+ e);
        }
        return orderDetailDO;
    }
}
